//获取应用实例
const app = getApp()

Component({
  properties: {
    admin:{
      type: Boolean,
      value: false,
      observer(newValue, oldValue) {
      }
    },

    defalutCurrent:{
      type: Object,
      value: {},
      observer(newValue, oldValue) {
        if (!!newValue && JSON.stringify(newValue) != "{}") {
          setTimeout(()=>{
            this.setData({
              currentClass: newValue
            })
          })
        } 
      }
    },
    type:String,
    classList: {
      type: Object,
      value:{},
      observer(newValue, oldValue){
        if ( !(JSON.stringify(newValue) == '[]' || JSON.stringify(newValue) == '{}') ) {
          if (this.data.type == 'group') {
            var currentJson = {
              name: '全部群组',
              cgId: 0,
            }

            this.setData({
              currentClass: currentJson
            })
          } else {
            this.assginValue(0)
          }
          
        }
      }
    }
  },
  data: {
    currentClass:{},
    allCLassState:false
  },

  methods: {
    // 班级切换操作
    classSwitch(e) {
      this.setData({
        allCLassState: !this.data.allCLassState
      })
    },

    // 切换班级
    switchClass(e) {
      var index = e.currentTarget.dataset.index

      this.setData({
        currentClass: this.data.classList[index]
      })
      
      this.triggerEvent('customevent', { 'currentClass': this.data.currentClass })

      // 操作赋值
      this.classSwitch()
    },

    // 群组全部操作
    switchGroup(e) {
      var index = e.currentTarget.dataset.index

      var currentJson = {
        name: '全部群组',
        cgId: 0,
      }

      this.setData({
        currentClass: currentJson
      })

      this.triggerEvent('customevent', { 'currentClass': this.data.currentClass })

      // 操作赋值
      this.classSwitch()
    },

    // editGroup编辑群组
    editGroup() {
      wx.navigateTo({
        url: `/pages/groupManager/groupManager`
      })
    },

    // 赋值操作
    assginValue(index) {
      var classlist = this.data.classList[index]

      this.setData({
        currentClass: classlist
      })
    },

    // 关闭
    hideFn() {
      this.setData({
        allCLassState: false
      })
    }
  }
})